package lab2;

import java.util.ArrayList;
import java.util.HashMap;

public class Graph {
        private HashMap<String, String[]> graph;
       
        public Graph(ArrayList<String> vertices) {
                graph = new HashMap();
               
                char[] vertex;
                String[] edges;
                int nbrOfChars = 0;
                int edgeNbr;
               
                for(int i = 0; i < vertices.size(); i++) {
                        edges = new String[vertices.size()];
                        edges[0] = "";
                        edgeNbr = 0;
                        vertex = vertices.get(i).toCharArray();
                        for(int j = 0; j < vertices.size(); j++) {
                                String v = vertices.get(j);
                                nbrOfChars = vertex.length - 1;
                                //System.out.println(vertices[j]);
                                if(j != i) {
                                        for(int k = 1; k < vertex.length; k++) {
                                                //System.out.print(vertex[k] + " ");
                                                if(v.contains("" + vertex[k])) {
                                                        nbrOfChars--;
                                                        v = v.replaceFirst("" + vertex[k], "");
                                                        //System.out.println(v);
                                                }
                                        }
                                }
                                if(nbrOfChars == 0) {
                                        edges[edgeNbr] = vertices.get(j);
                                        edgeNbr++;
                                }
                        }
                       
                        System.out.print("\n" + vertices.get(i) + ": ");
                        for(int j = 0; j < edges.length && edges[j] != null; j++) {
                                System.out.print(edges[j] + " ");
                               
                        }
                        System.out.println();
                        graph.put(vertices.get(i), edges);
                }
        }
       
        public String[] get(String key) {
                return graph.get(key);
        }
}
